/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wargame.controller;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import wargame.entity.*;
import module.*;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;
import wargame.dao.EmployeeDAO;

/**
 *
 * @author stanley.2007
 */
@Controller
@RequestMapping(value = "/employee")
public class EmployeeController {

    // accessed through http://localhost:8080/wargaming/servlet/employee/view
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public ModelAndView viewEmployee(Model model,
            @RequestParam(value = "announcement", required = false) String announcement,
            HttpServletRequest request) {
        // model refer to object to pass to this page
        // check index.jsp for the ${username}

        ModelAndView mv = new ModelAndView();

        Player player = ManagePlayer.getThisPlayer(request);

        if (player == null) {
            mv.setViewName("register");
            return mv;
        }

        ArrayList<Employee> employee_list = new ArrayList<Employee>();
        employee_list = EmployeeDAO.retrievePlayerEmployees(player);

        ArrayList<Employee> employee_for_hire_list = new ArrayList<Employee>();
        employee_for_hire_list = EmployeeDAO.retrieveTempEmployee();

//        for (int i = 0; i < 10; i++) {
//            Employee employee = new Employee();
//            employee.setHours_remaining(10);
//            employee.setMorale(9.8);
//            employee.setTolerance(2.1);
//            employee.setSalary(2000);
//            employee.setTurn_hired(2);
//            employee.setSkillPoints_jsp(2);
//            employee.setEmployee_id(1000);
//            employee_for_hire_list.add(employee);
//        }


        mv.addObject("player", player);
        mv.addObject("turn", GameTurn.getTurnInfo());
        mv.addObject("employee_list", employee_list);
        mv.addObject("employee_for_hire_list", employee_for_hire_list);
        if (announcement != null) {
            mv.addObject("announcement", announcement);
        }

        mv.setViewName("employee");

        return mv; // automatically mapped to employee.jsp
    }

    @RequestMapping(value = "/hireEmployee.do", method = RequestMethod.POST)
    public ModelAndView employEmployee(@RequestParam(value = "selectedEmployee", required = true) int selectedEmployee,
            HttpServletRequest request, HttpServletResponse response, Model model) {
        boolean isEmployed = false;
        Player p = ManagePlayer.getThisPlayer(request);
        ModelAndView mv = new ModelAndView();

        if(GameTurn.game_pause){
            mv.setViewName("redirect:/servlet/employee/view");
            mv.addObject("announcement", "game is paused. no actions is allowed");
            return mv;
        }

        isEmployed = ManageEmployee.hireEmployee(selectedEmployee, p);

        model.addAttribute("isEmployed", isEmployed);
        String announcement = null;
        if (isEmployed) {
            announcement = "you have successfully employed this guy";
        } else {
            announcement = "fail to employ. already taken by other player. hire faster next time.";
        }

        mv.setViewName("redirect:/servlet/employee/view");
        mv.addObject("announcement", announcement);

        return mv;
    }

    @RequestMapping(value = "/{employee_id}/train/{skill}", method = RequestMethod.GET)
    public ModelAndView trainEmployee(@PathVariable int employee_id,
            @PathVariable String skill, HttpServletRequest request, Model model) {
        ModelAndView mv = new ModelAndView();

        if(GameTurn.game_pause){
            mv.setViewName("redirect:/servlet/employee/view");
            mv.addObject("announcement", "game is paused. no actions is allowed");
            return mv;
        }

        Player p = ManagePlayer.getThisPlayer(request);
        Employee levelUpEmployee = EmployeeDAO.retrieveEmployee(employee_id);
        String announcement = ManageEmployee.trainEmployee(levelUpEmployee, p, skill);

        mv.setViewName("redirect:/servlet/employee/view");
        mv.addObject("announcement", announcement);
        return mv;
    }

    @RequestMapping(value = "/{employee_id}/fire", method = RequestMethod.GET)
    public ModelAndView fireEmployee(@PathVariable("employee_id") int employee_id,
            HttpServletRequest request, Model model) {

        Employee employee = EmployeeDAO.retrieveEmployee(employee_id);
        Player player = ManagePlayer.getThisPlayer(request);
        new ManageEmployee().fireEmployee(employee, player);

        ModelAndView mv = new ModelAndView();

        if(GameTurn.game_pause){
            mv.setViewName("redirect:/servlet/employee/view");
            mv.addObject("announcement", "game is paused. no actions is allowed");
            return mv;
        }
        
        mv.setViewName("redirect:/servlet/employee/view");

        return mv;
    }
}
